package cn.tedu.jxc.controller;

import cn.tedu.jxc.aop.LogAnnotation;
import cn.tedu.jxc.mapper.LogMapper;
import cn.tedu.jxc.pojo.dto.TimeDTO;
import cn.tedu.jxc.pojo.entity.Log;
import cn.tedu.jxc.pojo.vo.UserStandardVO;
import cn.tedu.jxc.service.LogService;
import cn.tedu.jxc.web.JsonPage;
import cn.tedu.jxc.web.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/logs")
@Slf4j
public class LogController {
    @Autowired
    private LogService logService;
    @Autowired
    private LogMapper mapper;

    @RequestMapping("list")
    @PreAuthorize("hasAuthority('/admin/read')")
    public JsonResult<JsonPage<Log>> getLogList(Integer page,Integer pageSize){
        JsonPage<Log> list = logService.getList(page, pageSize);
        log.debug("获取操作日志列表");
        return JsonResult.ok(list);
    }
    @PostMapping("/search-date")
    public JsonResult<JsonPage<Log>> selectByDate(TimeDTO timeDTO){
        log.debug("获取的时间为:{},{},{}",timeDTO.getDateTime(),timeDTO.getPage(),timeDTO.getPageSize());
        PageHelper.startPage(timeDTO.getPage(),timeDTO.getPageSize());
        List<Log> list = mapper.selectByDate(timeDTO.getDateTime());
        return JsonResult.ok(JsonPage.restPage(new PageInfo<>(list)));

    }

    @PostMapping("/deleteAll")
    public JsonResult<Void> clearLogs(){
        mapper.clearLogs();
        log.debug("已清空日志");
        return JsonResult.ok();
    }
}
